package com.jintian.smart.kernel.core.flow;

import com.jintian.smart.kernel.core.auth.IOperator;

public enum FlowAction {
    complete("完成"), fallback("回退"), transform("转发"), claim("抢占"), unclaim("撤销"), withdraw("取回"), abortTask("作废"),
    start("启动", ActionType.process), abort("作废", ActionType.process), suspend("挂起", ActionType.process), resume("解挂", ActionType.process), delete("删除", ActionType.process),
    query("查询", ActionType.flow);

    public static final String VARNAME_START_MODELNAME = "modelName";
    public static final String VARNAME_START_BIZCLASS = "bizClass";
    public static final String VARNAME_START_TITLE = "title";
    public static final String VARNAME_START_AREANAME = "areaName";

    public static final String TRANSIENT_VARNAME_BIZBEAN = "bizBean";
    public static final String TRANSIENT_VARNAME_OPERATOR = "operator";

    public static final String VARNAME_ABORT = "abort";
    public static final String VARNAME_ABORT_REASON = VARNAME_ABORT + "Reason";
    public static final String VARNAME_ABORT_PERSONID = VARNAME_ABORT + "PersonId";
    public static final String VARNAME_ABORT_PERSONNAME = VARNAME_ABORT + "PersonName";
    // 启动后立即流转
    public static final String VARNAME_COMPLETE_AFTER_START = "complete_after_start";
    public static final String VARNAME_COMPLETE_AFTER_RESUME = "complete_after_resume";

    //    private final boolean process;
    private final String title;
    private final ActionType actionType;

    private FlowAction(String title, ActionType actionType) {
        this.title = title;
        this.actionType = actionType;
//        this.process = process;
    }

    private FlowAction(String title) {
        this(title, ActionType.task);
    }

    public String getTitle() {
        return title;
    }

//    public boolean isProcess() {
//        return process;
//    }

    public boolean isActionType(ActionType actionType) {
        if (this.actionType == actionType) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return FlowAction.class.getSimpleName() + "." + this.name();
    }

    private static final ThreadLocal<IOperator> flowOwner = new ThreadLocal<IOperator>();

    /**
     * 绑定委托人，当前登录为执行者。必须unbindFlowOwner
     *
     * @param user
     */
    public static void bindFlowOwner(IOperator user) {
        flowOwner.set(user);
    }

    public static void unbindFlowOwner() {
        flowOwner.set(null);
    }

    public static IOperator getFlowOwner() {
        return flowOwner.get();
    }
}

